package exchange;

import org.bukkit.ChatColor;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.FixedSetPrompt;
import org.bukkit.conversations.Prompt;

public class ExchangePrompt extends FixedSetPrompt {
	
	public ExchangePrompt(ItemList items) {
		super();
	}

	@Override
	public String getPromptText(ConversationContext context) {
		if (context.getSessionData("reset") != null) {
			Item item = (Item) context.getSessionData("item");
			int quantity = (int) context.getSessionData("reset");
			item.increaseQuantity(quantity);
			context.setSessionData("reset", 0);
		}
		context.getSessionData("page") == ? 1; 
		String info = "\n" + BLUE + "Item" + YELLOW + ": "
				+ GREEN + "Buy Price " + YELLOW
				+ "/ " + RED + "Sell Price" 
				+ YELLOW + " / " + WHITE + "Current Supply";
		
		for(String item : fixedSet){
			info = info + "\n" + item.toString();
		}
		return "Welcome to the Server Exchange!" + info;
	}

	@Override
	protected boolean isInputValid(ConversationContext context, String input) {
		input = input.toLowerCase();
		if (input.equals("/balance") || input.equals("/money") || input.equals("/cancel")) {
			return true;
		}

		String[] query = input.split(" ");
		if (query.length != 3) {
			context.setSessionData("error", "arguments length");
			return false;
		}

		boolean validCommand = query[0].equals("/buy") || query[0].equals("/sell");
		if (!validCommand) {
			context.setSessionData("error", "invalid command");
			return false;
		}

		boolean validItem = fixedSet.contains(query[1]);
		if (!validItem) {
			context.setSessionData("error", "invalid item");
			return false;
		}

		boolean validQuantity = false;
		try {
			validQuantity = Integer.parseInt(query[2]) > 0;
			if (!validQuantity) {
				context.setSessionData("error", "quantity below 1");
				return false;
			}
		}
		catch (NumberFormatException e) {
			context.setSessionData("error", "number format");
			return false;
		}

		Player player = (Player) context.getForWhom();
		Item item = Item.getItem(query[1]);
		int quantity = Integer.parseInt(query[2]);
		
		switch (query[0]) {
		case "/buy":
			if (quantity > item.currentSupply()) {
				context.setSessionData("error", "not enough supply");
				return false;
			}
			
			int price = quantity * item.buyPrice();
			if (price > Money.getMoney(player).balance()) {
				context.setSessionData("error", "not enough money");
				return false;
			}
			
			Inventory check = Bukkit.createInventory(null, InventoryType.PLAYER);
			check.setContents(player.getInventory().getContents());
			if (!check.addItem(item.getItemStack(quantity)).isEmpty()) {
				context.setSessionData("error", "inventory space");
				return false;
			}
			break;
		case "/sell":
			if (!player.getInventory().containsAtLeast(item.getItemStack(0), quantity)) {
				context.setSessionData("error", "invalid sale");
				return false;
			}
			break;
		}

		return validCommand && validItem && validQuantity;
	}

	@Override
	protected Prompt acceptValidatedInput(ConversationContext context, String input) {
		input = input.toLowerCase();
		if (input.equals("/cancel")) {
			return Prompt.END_OF_CONVERSATION;
		}
		else if (input.equals("/balance") || input.equals("/money")) {
			Player player = (Player) context.getForWhom();
			Money money = Money.getMoney(player);
			player.sendMessage(money.toString());
			return this;
		}

		String[] query = input.split(" ");
		Item item = Item.getItem(query[1]);
		context.setSessionData("item", item);
		int quantity = Integer.parseInt(query[2]);
		context.setSessionData("quantity", quantity);
		
		switch (query[0]) {
		case "/buy":
			context.setSessionData("type", "buy");
			context.setSessionData("price", item.buyPrice() * quantity);
			context.setSessionData("reset", quantity);
			item.decreaseSupply(quantity);
			break;
		case "/sell":
			context.setSessionData("type", "sell");
			context.setSessionData("price", item.sellPrice() * quantity);
			break;
		}

		return new transactionPrompt();
	}
	
	@Override
	protected String getFailedValidationText(ConversationContext context, String input) {
		String error = (String) context.getSessionData("error");
		switch (error) {
		case "arguments length":
			return "You did not type all parameters for a sale (Missing /buy or /sell, an item, or a quantity)";
		case "invalid command":
			return "You did not type /buy, /sell, or cancel";
		case "invalid item":
			return "Not a valid item";
		case "quantity below 1":
			return "You must input a quantity of at least 1";
		case "number format":
			return "You did not input a number";
		case "not enough money":
			return "You do not have enough money";
		case "inventory space":
			return "You do not have enough space in your inventory";
		case "not enough supply":
			return "The Exchange does not have that quantity to buy";
		case "invalid sale":
			return "You do not have the necessary items to sell";
		default:
			return "";
		}
	}

}
